{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy部分作业"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业要求"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.随机数生成六个班的考试成绩，3门考试：Python、数学、语文。每个班50人\n",
    "\n",
    "2.将六个班的考试成绩进行合并得到score\n",
    "\n",
    "3.生成性别数组sex，水平叠加数组sex和score得到data\n",
    "\n",
    "4.分别计算男女生各科成绩统计指标：最小值、最大值、平均分、中位数、标准差"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 50, 3)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机生成6个班,每班50人,每人3门成绩数据\n",
    "random_data = np.random.randint(60, 150, size=(6, 50, 3))\n",
    "random_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 3)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 班级维度合并数据\n",
    "score = np.concatenate(random_data, axis=0)\n",
    "score.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 1)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成性别数据\n",
    "sex = np.random.randint(0, 2, size=(300, 1))\n",
    "sex.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(300, 4)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 合并score和sex得到data\n",
    "data = np.concatenate([score, sex], axis=1)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 69, 141, 102,   1],\n",
       "       [124, 111, 149,   1],\n",
       "       [144, 115, 132,   1],\n",
       "       [138, 126,  96,   1],\n",
       "       [ 93,  71,  77,   1],\n",
       "       [107,  71, 106,   1],\n",
       "       [128, 131, 147,   1],\n",
       "       [135, 107,  89,   1],\n",
       "       [ 86, 108,  71,   1],\n",
       "       [139, 148,  61,   1],\n",
       "       [144, 104,  67,   1],\n",
       "       [ 87,  96,  77,   1],\n",
       "       [ 81, 131, 139,   1],\n",
       "       [ 73, 127, 108,   1],\n",
       "       [101, 102, 118,   1],\n",
       "       [ 63, 149,  73,   1],\n",
       "       [ 74,  72, 101,   1],\n",
       "       [ 90, 127,  99,   1],\n",
       "       [128, 113, 136,   1],\n",
       "       [ 62, 108, 121,   1],\n",
       "       [ 83,  60, 136,   1],\n",
       "       [ 64, 104,  92,   1],\n",
       "       [ 97,  66, 117,   1],\n",
       "       [ 81,  93,  85,   1],\n",
       "       [144, 128,  65,   1],\n",
       "       [ 76, 104, 117,   1],\n",
       "       [115, 116, 146,   1],\n",
       "       [133,  65, 130,   1],\n",
       "       [149,  90, 144,   1],\n",
       "       [ 66, 119, 134,   1],\n",
       "       [113, 109,  70,   1],\n",
       "       [146,  73,  81,   1],\n",
       "       [ 95,  70,  78,   1],\n",
       "       [112, 118, 134,   1],\n",
       "       [144,  95, 125,   1],\n",
       "       [ 97,  82, 136,   1],\n",
       "       [122,  99, 131,   1],\n",
       "       [139,  76,  75,   1],\n",
       "       [ 85, 138,  94,   1],\n",
       "       [ 72, 140,  61,   1],\n",
       "       [ 66, 149,  90,   1],\n",
       "       [108, 132,  98,   1],\n",
       "       [ 87,  69, 120,   1],\n",
       "       [105,  80,  93,   1],\n",
       "       [133,  88, 133,   1],\n",
       "       [ 74, 103,  65,   1],\n",
       "       [124,  63,  99,   1],\n",
       "       [127, 148, 135,   1],\n",
       "       [141, 140, 101,   1],\n",
       "       [ 81,  70, 126,   1],\n",
       "       [131,  67,  85,   1],\n",
       "       [125,  93, 107,   1],\n",
       "       [131, 119, 111,   1],\n",
       "       [ 69, 144, 124,   1],\n",
       "       [112,  71,  97,   1],\n",
       "       [130, 115, 108,   1],\n",
       "       [ 84,  81, 142,   1],\n",
       "       [105, 130,  77,   1],\n",
       "       [ 88, 147, 123,   1],\n",
       "       [ 76,  99, 140,   1],\n",
       "       [ 79,  71, 133,   1],\n",
       "       [104,  66,  66,   1],\n",
       "       [102,  74,  87,   1],\n",
       "       [148,  96,  69,   1],\n",
       "       [109,  77, 111,   1],\n",
       "       [ 62,  64, 113,   1],\n",
       "       [ 85,  79, 104,   1],\n",
       "       [ 84, 133,  92,   1],\n",
       "       [ 71, 143, 148,   1],\n",
       "       [ 69, 147,  83,   1],\n",
       "       [128,  98,  81,   1],\n",
       "       [102, 127,  92,   1],\n",
       "       [ 78,  64, 148,   1],\n",
       "       [ 92, 114,  79,   1],\n",
       "       [102,  97, 127,   1],\n",
       "       [ 88,  96, 100,   1],\n",
       "       [112, 142,  64,   1],\n",
       "       [122,  64, 112,   1],\n",
       "       [ 60,  83,  67,   1],\n",
       "       [ 99,  77,  67,   1],\n",
       "       [135,  84,  82,   1],\n",
       "       [102,  93, 147,   1],\n",
       "       [107,  81,  96,   1],\n",
       "       [114,  91,  99,   1],\n",
       "       [ 74, 136, 129,   1],\n",
       "       [ 77, 147, 110,   1],\n",
       "       [111, 117, 108,   1],\n",
       "       [109, 130, 140,   1],\n",
       "       [129,  92,  77,   1],\n",
       "       [138,  81, 116,   1],\n",
       "       [136, 140,  97,   1],\n",
       "       [122,  94,  79,   1],\n",
       "       [ 73,  78,  79,   1],\n",
       "       [115,  73, 121,   1],\n",
       "       [135, 112,  85,   1],\n",
       "       [ 70, 131,  64,   1],\n",
       "       [131,  66, 137,   1],\n",
       "       [ 81,  91, 137,   1],\n",
       "       [132,  81, 132,   1],\n",
       "       [108,  87,  76,   1],\n",
       "       [136, 112,  75,   1],\n",
       "       [ 74, 114, 146,   1],\n",
       "       [129, 130, 103,   1],\n",
       "       [132,  85,  69,   1],\n",
       "       [ 93,  87,  77,   1],\n",
       "       [ 93,  77, 128,   1],\n",
       "       [147, 110,  83,   1],\n",
       "       [ 89,  85,  70,   1],\n",
       "       [ 62,  86, 130,   1],\n",
       "       [ 63,  82, 116,   1],\n",
       "       [ 76, 145, 134,   1],\n",
       "       [130, 133, 101,   1],\n",
       "       [ 81,  95,  61,   1],\n",
       "       [ 83, 146,  61,   1],\n",
       "       [111, 120,  71,   1],\n",
       "       [ 86, 121, 128,   1],\n",
       "       [ 67,  62, 117,   1],\n",
       "       [ 86,  76, 137,   1],\n",
       "       [130, 132,  85,   1],\n",
       "       [141,  92, 114,   1],\n",
       "       [130, 148,  73,   1],\n",
       "       [128, 113,  61,   1],\n",
       "       [137,  84,  77,   1],\n",
       "       [121,  92, 138,   1],\n",
       "       [104,  62, 108,   1],\n",
       "       [116,  99, 146,   1],\n",
       "       [103, 144, 147,   1],\n",
       "       [139, 147,  82,   1],\n",
       "       [145, 143, 113,   1],\n",
       "       [124,  78, 113,   1],\n",
       "       [ 67, 119, 131,   1],\n",
       "       [ 82, 147, 103,   1],\n",
       "       [137, 115,  93,   1],\n",
       "       [131, 119, 148,   1],\n",
       "       [ 67,  71,  79,   1],\n",
       "       [ 77,  61, 100,   1],\n",
       "       [ 68,  71, 117,   1],\n",
       "       [118,  73,  64,   1],\n",
       "       [126, 110, 128,   1],\n",
       "       [ 77, 128,  90,   1],\n",
       "       [143,  99, 141,   1],\n",
       "       [142,  65, 126,   1],\n",
       "       [138, 146,  73,   1],\n",
       "       [116,  99,  91,   1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[117,  60, 133,   0],\n",
       "       [129, 148,  80,   0],\n",
       "       [103,  87,  74,   0],\n",
       "       [127, 117,  70,   0],\n",
       "       [124, 119,  78,   0],\n",
       "       [ 75, 114, 112,   0],\n",
       "       [113, 139, 122,   0],\n",
       "       [107, 101, 100,   0],\n",
       "       [108,  87,  93,   0],\n",
       "       [ 91,  83, 119,   0],\n",
       "       [116,  83,  75,   0],\n",
       "       [107, 134, 128,   0],\n",
       "       [ 67, 149,  68,   0],\n",
       "       [133, 111, 149,   0],\n",
       "       [108,  99,  60,   0],\n",
       "       [ 61, 130, 147,   0],\n",
       "       [ 98, 135, 122,   0],\n",
       "       [104, 136,  95,   0],\n",
       "       [100,  92,  69,   0],\n",
       "       [130,  71,  83,   0],\n",
       "       [ 74,  72, 123,   0],\n",
       "       [123, 118, 122,   0],\n",
       "       [141, 126, 121,   0],\n",
       "       [101,  93,  79,   0],\n",
       "       [ 90, 108,  64,   0],\n",
       "       [ 90, 136, 134,   0],\n",
       "       [ 74, 118, 113,   0],\n",
       "       [ 89,  65, 104,   0],\n",
       "       [113,  64,  74,   0],\n",
       "       [ 64,  92, 128,   0],\n",
       "       [ 90, 138,  78,   0],\n",
       "       [ 94,  76,  66,   0],\n",
       "       [109,  68, 113,   0],\n",
       "       [ 67, 101,  85,   0],\n",
       "       [103,  99, 125,   0],\n",
       "       [ 81,  75, 131,   0],\n",
       "       [136,  80,  77,   0],\n",
       "       [134,  87, 105,   0],\n",
       "       [148, 112, 120,   0],\n",
       "       [ 90, 100,  73,   0],\n",
       "       [118,  70, 141,   0],\n",
       "       [ 89, 147,  88,   0],\n",
       "       [ 82, 114, 123,   0],\n",
       "       [ 62, 121,  76,   0],\n",
       "       [ 86, 130,  99,   0],\n",
       "       [ 67, 109, 122,   0],\n",
       "       [119, 124, 140,   0],\n",
       "       [101,  92, 116,   0],\n",
       "       [126,  92, 136,   0],\n",
       "       [117, 144,  92,   0],\n",
       "       [ 92, 137,  98,   0],\n",
       "       [133, 106, 118,   0],\n",
       "       [117, 110,  73,   0],\n",
       "       [130, 134,  88,   0],\n",
       "       [ 87, 145, 122,   0],\n",
       "       [ 94, 117, 132,   0],\n",
       "       [140,  65, 110,   0],\n",
       "       [ 83, 105, 133,   0],\n",
       "       [135, 124,  87,   0],\n",
       "       [139,  87, 117,   0],\n",
       "       [ 88,  76, 133,   0],\n",
       "       [101, 116, 103,   0],\n",
       "       [111,  95,  67,   0],\n",
       "       [149, 127,  97,   0],\n",
       "       [135,  95, 128,   0],\n",
       "       [ 87,  71,  97,   0],\n",
       "       [ 87,  73,  63,   0],\n",
       "       [106,  76, 102,   0],\n",
       "       [ 76,  60,  92,   0],\n",
       "       [118,  72, 129,   0],\n",
       "       [ 69, 113, 114,   0],\n",
       "       [ 74,  64, 105,   0],\n",
       "       [ 60,  87,  72,   0],\n",
       "       [ 73,  65,  64,   0],\n",
       "       [108,  98,  82,   0],\n",
       "       [ 97,  75,  61,   0],\n",
       "       [ 64, 126, 123,   0],\n",
       "       [ 81,  81, 146,   0],\n",
       "       [135, 140, 134,   0],\n",
       "       [145, 136, 118,   0],\n",
       "       [ 62,  74, 111,   0],\n",
       "       [149, 145,  61,   0],\n",
       "       [ 90,  92, 103,   0],\n",
       "       [ 83,  92,  95,   0],\n",
       "       [ 93, 136,  74,   0],\n",
       "       [ 97, 146,  97,   0],\n",
       "       [137, 123,  60,   0],\n",
       "       [ 78, 133,  72,   0],\n",
       "       [146, 115,  60,   0],\n",
       "       [128,  67, 122,   0],\n",
       "       [125, 112, 115,   0],\n",
       "       [ 75,  72, 149,   0],\n",
       "       [125, 141,  70,   0],\n",
       "       [106,  79, 113,   0],\n",
       "       [122,  93, 103,   0],\n",
       "       [129, 100,  99,   0],\n",
       "       [ 99,  85, 109,   0],\n",
       "       [110, 124,  86,   0],\n",
       "       [114, 126,  79,   0],\n",
       "       [ 75, 117, 110,   0],\n",
       "       [ 62,  82, 105,   0],\n",
       "       [101, 141, 118,   0],\n",
       "       [ 98, 105,  89,   0],\n",
       "       [ 64,  68, 111,   0],\n",
       "       [ 67,  86,  60,   0],\n",
       "       [119, 123, 140,   0],\n",
       "       [102, 145,  94,   0],\n",
       "       [ 82, 104,  78,   0],\n",
       "       [ 84, 129,  87,   0],\n",
       "       [ 91, 138, 114,   0],\n",
       "       [ 96,  91, 136,   0],\n",
       "       [120,  93,  79,   0],\n",
       "       [121,  60, 105,   0],\n",
       "       [ 64, 117, 139,   0],\n",
       "       [ 69, 120,  91,   0],\n",
       "       [102, 125,  80,   0],\n",
       "       [ 68, 117,  63,   0],\n",
       "       [ 65,  81, 105,   0],\n",
       "       [ 98, 104,  82,   0],\n",
       "       [132,  61,  70,   0],\n",
       "       [103, 134, 136,   0],\n",
       "       [142, 131,  97,   0],\n",
       "       [ 85, 134,  63,   0],\n",
       "       [121,  94,  93,   0],\n",
       "       [ 93, 126,  99,   0],\n",
       "       [ 61, 107, 125,   0],\n",
       "       [137,  79, 141,   0],\n",
       "       [ 68,  91, 122,   0],\n",
       "       [ 92, 100,  84,   0],\n",
       "       [ 85,  83, 113,   0],\n",
       "       [ 69,  60,  89,   0],\n",
       "       [117, 144, 139,   0],\n",
       "       [136, 112, 132,   0],\n",
       "       [116, 119,  88,   0],\n",
       "       [103, 110,  75,   0],\n",
       "       [ 82, 141, 136,   0],\n",
       "       [ 83,  86, 128,   0],\n",
       "       [ 77,  78,  85,   0],\n",
       "       [106, 110, 137,   0],\n",
       "       [124, 114,  71,   0],\n",
       "       [104, 110, 125,   0],\n",
       "       [120, 132, 114,   0],\n",
       "       [109,  78,  84,   0],\n",
       "       [117, 122,  86,   0],\n",
       "       [148, 141,  85,   0],\n",
       "       [112, 116, 124,   0],\n",
       "       [ 70, 103, 149,   0],\n",
       "       [ 98,  87,  70,   0],\n",
       "       [147, 123,  96,   0],\n",
       "       [108, 100, 144,   0],\n",
       "       [136,  64, 131,   0],\n",
       "       [139, 124, 102,   0],\n",
       "       [133,  81, 121,   0],\n",
       "       [ 63, 119, 135,   0],\n",
       "       [125,  92,  66,   0],\n",
       "       [122, 131, 117,   0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 定义sex=0是女生，sex=1是男生\n",
    "cond_gril = data[:, 3] == 0\n",
    "cond_boy = data[:, 3] == 1\n",
    "\n",
    "# 根据条件索引筛选\n",
    "data_gril = data[cond_gril]\n",
    "data_boy = data[cond_boy]\n",
    "\n",
    "display(data_boy, data_gril)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(data_boy[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "boy score:\n",
      "Python Min: 60\n",
      "Math Min: 60\n",
      "Chinese Min: 61\n",
      "Python Max: 149\n",
      "Math Max: 149\n",
      "Chinese Max: 149\n",
      "Python Mean: 105\n",
      "Math Mean: 103\n",
      "Chinese Mean: 104\n",
      "Python Median: 106\n",
      "Math Median: 99\n",
      "Chinese Median: 102\n",
      "Python Std: 26\n",
      "Math Std: 26\n",
      "Chinese Std: 26\n",
      "————————————\n",
      "gril score\n",
      "Python Min: 60\n",
      "Math Min: 60\n",
      "Chinese Min: 60\n",
      "Python Max: 149\n",
      "Math Max: 149\n",
      "Chinese Max: 149\n",
      "Python Mean: 102\n",
      "Math Mean: 105\n",
      "Chinese Mean: 102\n",
      "Python Median: 102\n",
      "Math Median: 106\n",
      "Chinese Median: 103\n",
      "Python Std: 24\n",
      "Math Std: 24\n",
      "Chinese Std: 25\n"
     ]
    }
   ],
   "source": [
    "def print_calc(data):\n",
    "    print('Python Min: %d' % np.min(data[:, 0]))\n",
    "    print('Math Min: %d' % np.min(data[:, 1]))\n",
    "    print('Chinese Min: %d' % np.min(data[:, 2]))\n",
    "    print('Python Max: %d' % np.max(data[:, 0]))\n",
    "    print('Math Max: %d' % np.max(data[:, 1]))\n",
    "    print('Chinese Max: %d' % np.max(data[:, 2]))\n",
    "    print('Python Mean: %d' % np.mean(data[:, 0]))\n",
    "    print('Math Mean: %d' % np.mean(data[:, 1]))\n",
    "    print('Chinese Mean: %d' % np.mean(data[:, 2]))\n",
    "    print('Python Median: %d' % np.median(data[:, 0]))\n",
    "    print('Math Median: %d' % np.median(data[:, 1]))\n",
    "    print('Chinese Median: %d' % np.median(data[:, 2]))\n",
    "    print('Python Std: %d' % np.std(data[:, 0]))\n",
    "    print('Math Std: %d' % np.std(data[:, 1]))\n",
    "    print('Chinese Std: %d' % np.std(data[:, 2]))\n",
    "\n",
    "\n",
    "print('boy score:')\n",
    "print_calc(data_boy)\n",
    "print('————————————')\n",
    "print('gril score')\n",
    "print_calc(data_gril)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "aghanim",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
